//
//  ViewController.m
//  flowLaout控件封装
//
//  Created by 涂永江 on 2017/8/28.
//  Copyright © 2017年 涂永江. All rights reserved.
//
#import "ViewController.h"
#import "HMHorizontalGridView.h"
#import <Masonry/Masonry.h>

@interface ViewController () <HMHorizontalGridViewDataSource,HMHorizontalGridViewDelegate>

@property (strong, nonatomic)   HMHorizontalGridView    *horizontalGridView;

@end

@implementation ViewController {
    NSInteger _count;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _count = 10 + arc4random_uniform(40);
    NSLog(@"_count:%zd",_count);
    
    self.horizontalGridView = [HMHorizontalGridView new];
    self.horizontalGridView.dataSource = self;
    self.horizontalGridView.delegate = self;
    [self.view addSubview:self.horizontalGridView];
    
    [self.horizontalGridView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(200);
    }];
    
    [self.horizontalGridView reloadData];
}

#pragma mark - HMHorizontalGridViewDataSource -
// 提供网格单元的数量
- (NSInteger)numberOfItemsInHorizontalGridView:(HMHorizontalGridView *)horizontalGridView {
    return _count;
}

// 提供供网格单元图标路径
- (NSURL *)horizontalGridView:(HMHorizontalGridView *)horizontalGridView urlForItemAtIndex:(NSInteger)idx {
    NSArray *urls = @[
                      @"http://img1.3lian.com/2015/w23/27/d/25.jpg",
                      @"http://img4.duitang.com/uploads/item/201412/05/20141205191118_WRAfW.jpeg",
                      @"http://img4.duitang.com/uploads/blog/201404/26/20140426191356_NLK3j.thumb.700_0.png",
                      @"http://f.hiphotos.baidu.com/zhidao/pic/item/77094b36acaf2eddde258bfa8e1001e9380193c9.jpg",
                      @"http://img4.duitang.com/uploads/item/201406/11/20140611202123_J2Nji.jpeg"
                      ];
    NSInteger arrIdx = idx % urls.count;
    
    return [NSURL URLWithString:urls[arrIdx]];
}

// 提供网格单元标题
- (NSString *)horizontalGridView:(HMHorizontalGridView *)horizontalGridView titleForItemAtIndex:(NSInteger)idx {
    return [NSString stringWithFormat:@"item %zd",idx];
}

#pragma mark - HMHorizontalGridViewDelegate -
// 提供行数
- (NSInteger)rowOfPerPageInHorizontalGridView:(HMHorizontalGridView *)horizontalGridView {
    return 2;
}

// 提供列数
- (NSInteger)columnOfPerPageInHorizontalGridView:(HMHorizontalGridView *)horizontalGridView {
    return 5;
}

// 用户点击触发
- (void)horizontalGridView:(HMHorizontalGridView *)horizontalGridView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"item %zd 被点击",index);
}

@end
